package com.itlzh.mall.user.controller;

import com.itlzh.mall.common.entity.UserInfo;
import com.itlzh.mall.common.utils.JwtUtils;
import com.itlzh.mall.user.service.ILoginService;
import com.itlzh.mall.common.dto.LoginDTO;
import com.itlzh.mall.common.result.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RequestMapping("/login")
@RestController
public class LoginController {
    @Autowired
    private ILoginService iLoginService;

    @PostMapping()
    public Result<UserInfo> login(@RequestBody LoginDTO loginDTO, HttpServletResponse  response){
        UserInfo login = iLoginService.login(loginDTO);
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId",  login.getId());
        String token = JwtUtils.generateToken(map);
        response.setHeader("Authorization", token);
        return Result.success();
    }
}
